package com.lzy.huijinjiazu.mvp.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerSearchSuccessComponent;
import com.lzy.huijinjiazu.di.module.SearchSuccessModule;
import com.lzy.huijinjiazu.mvp.contract.SearchSuccessContract;
import com.lzy.huijinjiazu.mvp.presenter.SearchSuccessPresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.GoodsAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SearchSuccessActivity extends BaseActivity<SearchSuccessPresenter> implements SearchSuccessContract.View {


    @BindView(R.id.ll_title)
    LinearLayout mLlTitle;
    @BindView(R.id.rv_list)
    RecyclerView mRvList;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.tv_search)
    TextView mTvSearch;
    private GoodsAdapter mAdapter;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerSearchSuccessComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .searchSuccessModule(new SearchSuccessModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_search_success; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initRecyclerView();
        initData();
    }

    private void initRecyclerView() {
        ArmsUtils.configRecyclerView(mRvList, new GridLayoutManager(this, 2));
        mRvList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                outRect.right = (itemPosition + 1) % 2 == 0 ? 0 : ArmsUtils.dip2px(SearchSuccessActivity.this, 5);
                outRect.left = (itemPosition + 1) % 2 == 0 ? ArmsUtils.dip2px(SearchSuccessActivity.this, 5) : 0;
                outRect.bottom = ArmsUtils.dip2px(SearchSuccessActivity.this, 10);
                if (itemPosition == 0 || itemPosition == 1) {
                    outRect.top = ArmsUtils.dip2px(SearchSuccessActivity.this, 10);
                }
            }
        });
        mAdapter = new GoodsAdapter();
        mRvList.setAdapter(mAdapter);
    }

    private void initData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            data.add("");
        }
        mAdapter.setNewData(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBarMarginTop(this, mLlTitle);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.iv_back, R.id.tv_search, R.id.iv_clear, R.id.iv_shopping_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:
                ArmsUtils.startActivity(SearchActivity.class);
                break;
            case R.id.iv_clear:
                mTvSearch.setText("请输入搜索内容");
                break;
            case R.id.iv_shopping_cart:
                ArmsUtils.startActivity(ShoppingCartActivity.class);
                break;
        }
    }

}
